import numpy as np

def step_function(x):
    y = x > 0
    return y.astype(np.int)
    

def sigmoid(x):
    return 1 / (1 + np.exp(-x))
    
def relu(x):
    return np.maximum(0,x)
    

def identity_function(x):
    return x
    
def softmax(a):
    c = np.max(a)
    exp_a = np.exp(a - c)  #溢出对策
    sum_exp_a = np.sum(exp_a)
    y = exp_a / sum_exp_a
    
    return y

def mean_squared_error(y, t): 
    '''均方误差'''
    return 0.5 * np.sum((y-t)**2)


def cross_entropy_error_old(y, t): 
    '''交叉熵误差'''
    delta = 1e-7
    return -np.sum(t * np.log(y + delta))

def cross_entropy_error_old(y, t): 
    '''交叉熵误差'''
    delta = 1e-7
    return -np.sum(t * np.log(y + delta))

# def cross_entropy_error(y, t): 
#     '''交叉熵误差'''
#     if y.dim == 1:
#         t = t.reshape(1, t.size)
#         y = y.reshape(1, y.size)
#     batch_size = y.shape[0]
#     delta = 1e-7
#     return -np.sum(t * np.log(y + delta))

def cross_entropy_error(y, t):
    '''带mini-batch的交叉熵误差'''
    if y.ndim == 1:
        t = t.reshape(1, t.size)
        y = y.reshape(1, y.size)
        
    batch_size = y.shape[0]
    return -np.sum(t * np.log(y + 1e-7)) / batch_size